#include "mainwindow.h"
#include "tcpmgr.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    _login_dialog = new LoginDialog(this);
    _login_dialog->setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);

    setCentralWidget(_login_dialog);

    // 连接从登录页面跳转至注册页面的信号与槽
    connect(_login_dialog, &LoginDialog::sigSwitchRegister, this, &MainWindow::SlotSwitchReg);
    // 连接从登录页面跳转至重置密码页面的信号与槽
    connect(_login_dialog, &LoginDialog::sigSwitchReset, this, &MainWindow::SlotSwitchReset);
    // 连接从注册页面跳转至登录页面的信号与槽
    //    connect(_reg_dialog, &RegisterDialog::sigSwitchLogin, this, &MainWindow::SlotSwitchLogin);
    // 连接从重置密码页面跳转至登录页面的信号与槽
    //    connect(_reset_dialog, &ResetDialog::sigSwitchLogin, this, &MainWindow::SlotSwitchLoginFromReset);
    // 连接从登录界面跳转至聊天界面的信号与槽
    connect(TcpMgr::GetInstance().get(), &TcpMgr::sig_switch_chatdlg, this, &MainWindow::SlotSwitchChat);

    //    emit TcpMgr::GetInstance()->sig_switch_chatdlg();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::SlotSwitchReg()
{
    _reg_dialog = new RegisterDialog(this);
    _reg_dialog->setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
    connect(_reg_dialog, &RegisterDialog::sigSwitchLogin, this, &MainWindow::SlotSwitchLogin);
    setCentralWidget(_reg_dialog);
    _login_dialog->hide();  // 调用hide函数之后，qt对象树机制就会把相应对象(_login_dialog)回收
    _reg_dialog->show();
}

void MainWindow::SlotSwitchLogin()
{
    _login_dialog = new LoginDialog(this);
    _login_dialog->setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
    setCentralWidget(_login_dialog);
    _reg_dialog->hide();
    _login_dialog->show();
}

void MainWindow::SlotSwitchReset()
{
    _reset_dialog = new ResetDialog(this);
    _reset_dialog->setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
    connect(_reset_dialog, &ResetDialog::sigSwitchLogin, this, &MainWindow::SlotSwitchLoginFromReset);
    setCentralWidget(_reset_dialog);
    _login_dialog->hide();
    _reset_dialog->show();
}

void MainWindow::SlotSwitchLoginFromReset()
{
    _login_dialog = new LoginDialog(this);
    _login_dialog->setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
    setCentralWidget(_login_dialog);
    _reset_dialog->hide();
    _login_dialog->show();
}

void MainWindow::SlotSwitchChat()
{
    qDebug() << "接收到切换聊天界面的信号";
    _chat_dialog = new ChatDialog(this);
    _chat_dialog->setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
    setCentralWidget(_chat_dialog);
    _login_dialog->hide();
    _chat_dialog->show();
}
